//
//  ZJPAActionesTableView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/9/18.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJPAActionesTableView.h"
#import "ActivityJoinTableViewCell.h"
#import "ActivityJoinDetailModel.h"
#import "LPRedViewAndLableView.h"
#import "LPActivityDetailViewController.h"
#import "LPChooseActAndResouceTypeView.h"
#import "YBPopupMenu.h"
@interface ZJPAActionesTableView ()<ActivityJoinCellDelegate,LPCostomHeadLineAndTextViewAndTiShiDelegate,UITableViewDelegate,UITableViewDataSource,LPChooseActAndResouceTypeViewDelegate,YBPopupMenuDelegate>{
    /**  中间选择框  */
    NSInteger _chooseIndex;
    
}
/**  tableVie  */
@property (strong, nonatomic) UITableView  *tableView;
/**  悬浮按钮  */
@property (strong, nonatomic) UIButton  *meunButton;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;

/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;

/**  活动数量  */
@property (strong, nonatomic) NSString  *actNumber;
/**  label  */
@property (strong, nonatomic) UILabel  *header;

/**  jc  */
@property (strong, nonatomic) JCAlertView  *alertView;
/**  弹窗的model  */
@property (strong, nonatomic) ActivityJoinDetailModel  *tishiModel;
/**  弹窗section  */
@property (assign, nonatomic) NSInteger  tishiSection;
/**  弹窗row  */
@property (assign, nonatomic) NSInteger  tishiRow;

/** 选择框   */
@property (strong, nonatomic) LPChooseActAndResouceTypeView  *typeView;
/**  活动分类数组  */
@property (strong, nonatomic) NSArray  *typesArray;
/**  活动类型的BUTTON  */
@property (strong, nonatomic) UIButton  *typeButton;
/**    */
@property (strong, nonatomic) NSString  *bigClass;
/**    */
@property (strong, nonatomic) NSString  *smallClass;


@end

@implementation ZJPAActionesTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.bigClass = @"";
        self.smallClass = @"";
        self.typesArray = @[@"所有", @"组织生活", @"教育活动", @"社区公益"];
        
        [self layoutUI];

    }
    return self;
}

- (void)initDataSoure{
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page++;
        [weakSelf netWorking_footer];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - 数据

//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    
    NSString *GLOBALID2;
    if (ZJUSERINFORMODEL.DEPT_MODEL_JOB) {
        GLOBALID2 = ZJUSERINFORMODEL.DEPT_MODEL_JOB.GLOBALID;
    }else{
        GLOBALID2 = @"";
    }
    
    NSString *deptid;
    if ([[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.AUTH_STATUS isEqualToString:@"1"]) {//审核中
        deptid = @"-1";
    }else{
        deptid = ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID;
        
    }
    NSDictionary *paramer   = @{@"PAGE": page,
                                @"COUNT": @"10",
                                
                                @"GLOBALID1": ZJUSERINFORMODEL.DEPT_MODEL_BOOK.GLOBALID ? ZJUSERINFORMODEL.DEPT_MODEL_BOOK.GLOBALID :@"",
                                @"GLOBALID2": GLOBALID2,
                                @"keyword": @"",
                                @"DEPTID": deptid,
                                @"BIG_CLASS": self.bigClass,
                                @"SMALL_CLASS" : self.smallClass,
                                @"USER_ID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                                @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActListACT_NOT_OVER] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ACT_NOT_OVER"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[ActivityJoinDetailModel class] json:array] mutableCopy] ;
            [weakSelf.tableView reloadData];
            
        }
        
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.dataSouce removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
        [YJProgressHUD showSuccess:@"网络错误" inview:weakSelf];
    }];
    
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSString *GLOBALID2;
    if (ZJUSERINFORMODEL.DEPT_MODEL_JOB) {
        GLOBALID2 = ZJUSERINFORMODEL.DEPT_MODEL_JOB.GLOBALID;
    }else{
        GLOBALID2 = @"";
    }
    
    NSString *deptid;
    if ([[ZJUserInfoModel sharedUserInfo].USER_MODEL_JOB.AUTH_STATUS isEqualToString:@"1"]) {//审核中
        deptid = @"-1";
    }else{
        deptid = ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID;
        
    }
    NSDictionary *paramer   = @{@"PAGE": page,
                                @"COUNT": @"10",
                                @"GLOBALID1": ZJUSERINFORMODEL.DEPT_MODEL_BOOK.GLOBALID? ZJUSERINFORMODEL.DEPT_MODEL_BOOK.GLOBALID:@"",
                                @"GLOBALID2": GLOBALID2,
                                @"keyword": @"",
                                @"DEPTID": deptid,
                                @"BIG_CLASS": self.bigClass,
                                @"SMALL_CLASS" : self.smallClass,
                                @"USER_ID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                                @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};
    
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActListACT_NOT_OVER] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ACT_NOT_OVER"];
            [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[ActivityJoinDetailModel class] json:array]] ;
            [weakSelf.tableView reloadData];
            
            
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];

}


#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.typeView];
    [self addSubview:self.tableView];
    [self addSubview:self.meunButton];
    TenGrayShadowView *ten = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, self.width, 10 * [UIScreen zj_screenHScale])];
    ten.isRemoveTop = YES;
    self.tableView.tableHeaderView = ten;
}

#pragma mark - YBPopupMenuDelegate
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu{
    [self.typeButton setTitle:[NSString stringWithFormat:@"%@  ",self.typesArray[index]] forState:UIControlStateNormal];
    [UIButton moveButtonLeftTitleAndRightImage:self.typeButton];
    
    switch (index) {
        case 0:
            self.bigClass = @"";
            self.smallClass = @"";
            break;
        case 1:
            self.bigClass = @"1";
            self.smallClass = @"";
            break;
        case 2:
            self.bigClass = @"2";
            self.smallClass = @"5";
            break;
        case 3:
            self.bigClass = @"2";
            self.smallClass = @"6";
            break;
        case 4:
            self.bigClass = @"2";
            self.smallClass = @"7";
            break;
        case 5:
            self.bigClass = @"2";
            self.smallClass = @"8";
            break;
            
        default:
            break;
    }
    
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - LPChooseActAndResouceTypeViewDelegate
- (void)LPChooseActAndResouceTypeView_delegate_clickView:(UIButton *)sender clickIndex:(NSInteger)index{
    if (!self.typeButton) {
        self.typeButton = sender;
    }
    [YBPopupMenu showRelyOnView:sender titles:self.typesArray icons:nil menuWidth:sender.width * 0.4 showCount:0 delegate:self];
    _chooseIndex = index;
    
}

#pragma mark -ActivityJoinCellDelegate
- (void)ActivityJoinClick_index:(NSInteger)index section:(NSInteger)section statusText:(NSString *)statusText
{
    __weak typeof(self) weakSelf = self;
    ActivityJoinDetailModel *model = self.dataSouce[index];
    if ([statusText isEqualToString:@"加入"]) {
        [YJProgressHUD show:@"报名中..." inView:self mode:YJProgressModeLoading];
        /**  这是点击加入  */
        NSDictionary *paramer = @{@"AI_ID": model.ID,
                                  @"USER_ID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                                  @"USER_NAME": ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME};
        [NetWorkingManager postWithUrlString:[NetWorkingURL getactactionMobileEnroll] Parameter:paramer Success:^(id object) {
            NSString *CODE = [object objectForKey:@"CODE"];
            if ([CODE isEqualToString:@"20000"]) {
                [MobClick event:[UMMStatisticsString registerAct]];
                [YJProgressHUD showMsgWithoutView:@"报名成功"];
                [weakSelf.tableView.mj_header beginRefreshing];
                
            }else if ([CODE isEqualToString:@"80001"]) {
                [YJProgressHUD showMsgWithoutView:@"重复报名"];
            }else if ([CODE isEqualToString:@"80002"]) {
                [YJProgressHUD showMsgWithoutView:@"活动结束"];
            }else if ([CODE isEqualToString:@"80003"]) {
                [YJProgressHUD showMsgWithoutView:@"人数达上限"];
            }else if ([CODE isEqualToString:@"80004"]) {
                [YJProgressHUD showMsgWithoutView:@"无此活动或已被删除!"];
            }else{
                [YJProgressHUD showMsgWithoutView:@"报名失败"];
            }
            
        } Fail:^(id object) {
            [YJProgressHUD showMsgWithoutView:@"报名失败"];
        }];
    }else if([statusText isEqualToString:@"请假"]){
        
        LPCostomHeadLineAndTextViewAndTiShiView *tishi = [[LPCostomHeadLineAndTextViewAndTiShiView alloc] initWithFrame:CGRectMake(0, 0, self.width, [UIScreen zj_screenHeight]) headLine:@"请输入原因"];
        tishi.delegate = self;
        self.alertView = [[JCAlertView alloc] initWithCustomView:tishi dismissWhenTouchedBackground:YES];
        [self.alertView show];
        self.tishiModel = model;
        self.tishiSection = section;
        self.tishiRow = index;
        
    }
}

#pragma mark - LPCostomHeadLineAndTextViewAndTiShiDelegate
- (void)LPCostomHeadLineAndTextViewAndTiShiWithCancle:(BOOL)cancle{
    [self.alertView dismissWithCompletion:nil];
}

- (void)LPCostomHeadLineAndTextViewAndTiShiWithString:(NSString *)string{
    __weak typeof(self) weakSelf = self;
    /**   这里是请假  */
    [YJProgressHUD show:@"请假中..." inView:self mode:YJProgressModeLoading];
    NSDictionary *paramer = @{@"AI_ID": self.tishiModel.ID,
                              @"FROM_USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"FROM_USERNAME": ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME,
                              @"FROM_UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"PMID": ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID,
                              @"CONTENT": string,
                              @"TO_UNITID": self.tishiModel.UNITID,
                              @"FROM_UNITNAME": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.DEPT_NAME,
                              @"ACT_NAME": self.tishiModel.NAME};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActLeave] Parameter:paramer Success:^(id object) {
        if ([object[@"STATE"] isEqualToString:@"20000"]) {
            [YJProgressHUD showMsgWithoutView:@"请假成功"];
            /**  请假成功修改数据源刷新界面  */
            weakSelf.tishiModel.IS_LEAVE = @"1";
            weakSelf.tishiModel.LEAVE_STATE = @"0";
            weakSelf.dataSouce[self.tishiRow] = weakSelf.tishiModel;
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:weakSelf.tishiRow inSection:weakSelf.tishiSection];
            [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
            
        }else{
            [YJProgressHUD showMsgWithoutView:@"请假失败"];
        }
        
    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"请假失败"];
    }];
    
    [self.alertView dismissWithCompletion:nil];
    
}

#pragma mark - 点击事件
- (void)action_meunButton{
    if (self.delegate) {
        [self.delegate ZJPAActionesTableViewDelegate_viewClick];
    }
}


#pragma mark - /**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
    
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    ActivityJoinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[ActivityJoinTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        
    }
    cell.delegate = self;
    cell.index = indexPath.row;
    cell.section = indexPath.section;
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}

/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    __weak typeof(self) weakSelf = self;
    LPActivityDetailViewController *vc = [LPActivityDetailViewController new];
    vc.model  = self.dataSouce[indexPath.row];
    vc.block = ^{
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    [[self topViewController].navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40 * [UIScreen zj_screenHScale], self.width, self.height - 40 * [UIScreen zj_screenHScale]) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = [UIColor themeGrayColor];
    }
    return _tableView;
}


- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}



- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

- (LPChooseActAndResouceTypeView *)typeView{
    if (!_typeView) {
        _typeView = [[LPChooseActAndResouceTypeView alloc] initWithFrame:CGRectMake(0, 0, self.width, 40 * [UIScreen zj_screenHScale]) titles:@[@"活动分类  "]];
        _typeView.delegate = self;
    }
    return _typeView;
}

- (UIButton *)meunButton{
    if (_meunButton == nil){
        _meunButton = [[UIButton alloc]initWithFrame:CGRectMake(self.width - 65 * [UIScreen zj_screenWScale], self.height - 70 * [UIScreen zj_screenHScale], 50 * [UIScreen zj_screenHScale], 50 * [UIScreen zj_screenHScale])];
        _meunButton.backgroundColor = [UIColor whiteColor];
        _meunButton.layer.cornerRadius = _meunButton.width / 2;
        _meunButton.layer.borderWidth = 1;
        _meunButton.layer.borderColor = [UIColor zj_colorWithHexString:@"d2d2ca"].CGColor;
        [_meunButton setImage:[UIImage imageNamed:@"float_map"] forState:UIControlStateNormal];
        [_meunButton addTarget:self action:@selector(action_meunButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _meunButton;
}
@end
